﻿using ConfExpert.Forms.Base;
using DevExpress.XtraPivotGrid;
using DevTeam;

using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;

namespace ConfExpert.Forms.Planlama
{
    public partial class KesimRaporu : XForm
    {
        public KesimRaporu()
        {
            InitializeComponent();
        }

        BindingSource bs;
        private void KesimRaporu_Load(object sender, EventArgs e)
        {
            bs = gridView2.Init("GetPlanlamaPivot");
            gridView2.FormatAsDate("KesimTarihi");

            //DrillDownGrid(gridView2, bs);
            gridView2.Columns["Yil"].Visible = gridView2.Columns["Ay"].Visible = gridView2.Columns["Hafta"].Visible = gridView2.Columns["Gun"].Visible = false;
            gridView2.AddSummary("Qty");
            InitPivot(pivotGridControl1, bs);

            gridView2.SetFilterTypeToCheckedList("DosyaNo");

            radioGroup1.EditValueChanged += RadioGroup1_EditValueChanged;
            radioGroup1.EditValue = "10 GÜN";

            splitContainerControl2.AutoSize();
            ExpandCollapse(splitContainerControl2, AcKapa.Collapse);
            btnExcel.ItemClick += BtnExcel_ItemClick;
        }

        private void BtnExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //pivotGridControl1.OptionsPrint.PrintColumnHeaders =
            //pivotGridControl1.OptionsPrint.PrintDataHeaders =
            pivotGridControl1.OptionsPrint.PrintFilterHeaders = DevExpress.Utils.DefaultBoolean.False;
            ExportPivot(pivotGridControl1);
        }

        int BuYil = DateTime.Now.Year, 
            BuAy = DateTime.Now.Month,
            BuHafta = CultureInfo.CurrentUICulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday),
            BuGun = DateTime.Now.DayOfYear;
        private void RadioGroup1_EditValueChanged(object sender, EventArgs e)
        {
            switch (radioGroup1.EditValue.ToStr())
            {
                case "Tümü": bs.Filter = string.Empty; break;
                case "Bu Yıl": bs.Filter = "Yil=" + BuYil.ToString(); break;
                case "Bu Ay": bs.Filter = "Yil=" + BuYil.ToString() + " AND Ay=" + BuAy.ToString(); break;
                case "Bu Hafta": bs.Filter = "Yil=" + BuYil.ToString() + " AND Hafta=" + BuHafta.ToString(); break;
                case "10 GÜN": bs.Filter = "KesimTarihi >= '" + DateTime.Today.AddDays(-10).ToString("o", CultureInfo.InvariantCulture) + "'"; break;
            }
            pivotGridControl1.RefreshData();
        }

        void pivotGridControl1_CustomAppearance(object sender, PivotCustomAppearanceEventArgs e)
        {
            if (e.Focused)
                e.Appearance.BackColor = e.Appearance.BackColor2 = Color.YellowGreen;   // Color.FromArgb(255, 192, 0);
        }


        void pivotGridControl1_CellClick(object sender, DevExpress.XtraPivotGrid.PivotCellEventArgs e)
        {
            gridControl2.DataSource = e.CreateDrillDownDataSource();
            gridView2.Columns["Yil"].Visible = gridView2.Columns["Ay"].Visible = gridView2.Columns["Hafta"].Visible = gridView2.Columns["Gun"].Visible = false;
            gridView2.BestFitColumns();
            ExpandCollapse(splitContainerControl2, (gridView2.RowCount > 0 ? AcKapa.Expand : AcKapa.Collapse));
            gridView2.MoveFirst();
        }

        //int areaIndex = 0;
        bool PivotInitialized = false;
        void InitPivot(PivotGridControl pgc, BindingSource bs)
        {
            pgc.DataSource = bs;
            if (!PivotInitialized)
            {
                pgc.OptionsCustomization.CustomizationFormStyle = DevExpress.XtraPivotGrid.Customization.CustomizationFormStyle.Excel2007;
                //pgc.CellClick += new PivotCellEventHandler(pivotGridControl1_CellClick);
                pgc.FieldValueDisplayText += new PivotFieldDisplayTextEventHandler(pivotGrid_FieldValueDisplayText);
                pgc.CustomAppearance += new PivotCustomAppearanceEventHandler(pivotGridControl1_CustomAppearance);
                pgc.CellClick += new PivotCellEventHandler(pivotGridControl1_CellClick);
                pgc.BestFit();


                pgc.BeginUpdate();

                AddNewField(pgc, "AtelyeId", PivotArea.FilterArea).FieldEdit = RI(Rp.Atelye);
                AddNewField(pgc, "Model", PivotArea.FilterArea);
                AddNewField(pgc, "CustomerId", PivotArea.FilterArea).FieldEdit = RI(Rp.V_Customer);
                AddNewField(pgc, "DosyaNo", PivotArea.FilterArea);

                PivotGridField[] fields2 = new PivotGridField[4];
                //pivotGridControl1.Fields["Tarih"].ValueFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                //pivotGridControl1.Fields["Tarih"].ValueFormat.FormatString = "d";

                PivotGridField fieldYear = new PivotGridField("KesimTarihi", PivotArea.ColumnArea);
                PivotGridField fieldMonth = new PivotGridField("KesimTarihi", PivotArea.ColumnArea);
                PivotGridField fieldWeek = new PivotGridField("KesimTarihi", PivotArea.ColumnArea);
                PivotGridField fieldDay = new PivotGridField("KesimTarihi", PivotArea.ColumnArea);

                // Add the fields to the field collection.
                pgc.Fields.Add(fieldYear);
                fieldYear.GroupInterval = PivotGroupInterval.DateYear;
                fieldYear.Caption = "Yıl";

                pgc.Fields.Add(fieldMonth);
                fieldMonth.GroupInterval = PivotGroupInterval.DateMonth;
                fieldMonth.Caption = "Ay";

                pgc.Fields.Add(fieldWeek);
                fieldWeek.GroupInterval = PivotGroupInterval.DateWeekOfYear;
                fieldWeek.Caption = "Hafta";

                pgc.Fields.Add(fieldDay);
                fieldDay.GroupInterval = PivotGroupInterval.DateDay;
                fieldDay.Caption = "Gün";

                fields2[0] = fieldYear;
                fields2[1] = fieldMonth;
                fields2[2] = fieldWeek;
                fields2[3] = fieldDay;

                //fields2[2].ExpandedInFieldsGroup = fields2[3].ExpandedInFieldsGroup = false;
                pgc.Groups.Add(fields2, "Tarih");

                AddNewField(pgc, "AtelyeId", PivotArea.RowArea).FieldEdit = RI(Rp.Atelye);
                AddNewField(pgc, "Qty", PivotArea.DataArea);    //.Caption = "TotalQty";
                pgc.Fields["Qty"].CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                pgc.Fields["Qty"].CellFormat.FormatString = "#,###.##";
                //.Width = 40;  .Visible = true;
                //    fld.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Sum;

                foreach (PivotGridField pgf in pgc.Fields)
                {
                    pgf.SortOrder = PivotSortOrder.Ascending;
                    pgf.Options.AllowSort = DevExpress.Utils.DefaultBoolean.True;
                }
                pgc.EndUpdate();

                PivotInitialized = true;
            }
        }

    }
}